/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_YH50.h
 *
 *  Created on: 2022年8月7日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_YH50_H_
#define PERIPHERAL_LAYER_INC_LOS_YH50_H_

#include "LOS_MAX14830.h"
#include "LOS_State.h"

#ifdef NOW_LAUNCHPAD
#define YH50_MAXNUM LOS_MAX14830_NUM1
#else
#define YH50_MAXNUM LOS_MAX14830_NUM1
#endif

#define YH50_MAXPort 3

#define YH50_CMD_RESET 0x0000
#define YH50_CMD_DATA  0XE55E

typedef struct LOS_YH50STA_t
{
    uint8_t VAL;       //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;       //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;       //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;       //通信状态，通信正常为0，通信异常为其他值
    uint8_t SWNUM;
    uint8_t RBCNT;     //复位次数
    uint8_t NFR;       //Need For Reset 需要复位标志位，1为需要复位，0为不需要
    uint8_t RBT_F;     //表示单机是否处于重启过程中 0：未处于 1：处于重启过程中
    uint8_t RECOV;     //恢复计数：单机重启完成后，需要等待一段时间才能再次进入重启流程，将该计数置于某一值开始倒计时，倒计时结束后会再次开始判断是否需要重启
    uint8_t HL_TXF;    //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;   //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint32_t TMCNT;
    uint16_t FlagF_CNT;         //物理量异常重启计数
    uint16_t FlagT_CNT;         //通信异常重启计数
    uint16_t FlagY_CNT;         
    
    double x_gyro;
    double y_gyro;
    double z_gyro;
    float x_gyro_deg;
    float y_gyro_deg;
    float z_gyro_deg;
    double x_temp;  //X轴温度原始值（由姿控程序处理解算）
    double y_temp;  //Y轴温度原始值（由姿控程序处理解算）
    double z_temp;  //Z轴温度原始值（由姿控程序处理解算）
    double lumpow;  
    uint8_t Flag_D;
}LOS_YH50STA_t;

typedef struct LOS_YH50TEST_t
{
    uint8_t axis_velsel;  //角速度上注轴选择  0:不改变/恢复  1:X轴  2:Y轴  3:Z轴  4:开始作用
    uint8_t axis_tempsel; //温度上注轴选择    0:不改变/恢复  1:X轴  2:Y轴  3:Z轴  4:开始作用
    uint8_t lumsel;       //光功率上注选择    0:不改变/恢复  1:改变
    float lum_upt;        //光功率上注值
    float x_temp_upt;     //X轴温度上注值（用于地面测试故障）
    float y_temp_upt;     //Y轴温度上注值（用于地面测试故障）  
    float z_temp_upt;     //Z轴温度上注值（用于地面测试故障） 
    float x_gyro_upt;     //X轴角速度上注值（用于地面测试故障）
    float y_gyro_upt;     //Y轴角速度上注值（用于地面测试故障）
    float z_gyro_upt;     //Z轴角速度上注值（用于地面测试故障）
}LOS_YH50TEST_t;

extern LOS_YH50STA_t STA_YH50;
extern LOS_YH50TEST_t TEST_YH50;

extern uint8_t TMPACK_YH50[32];

void LOS_YH50_SendCMD(uint16_t cmd);
void LOS_YH50_PWROn();
void LOS_YH50_PWROff();
uint8_t LOS_YH50_DataProcess(uint8_t* data);

#endif /* PERIPHERAL_LAYER_INC_LOS_YH50_H_ */
